#include "TextureManager.h"

TextureManager::TextureManager()
{
	errorTexture.loadFromFile("img/error.png");
}

TextureManager::~TextureManager()
{

}


sf::Texture& TextureManager::get(const std::string name)
{
	for (std::map<std::string, sf::Texture>::iterator it = m_textures.begin();
		it != m_textures.end();
		++it)
	{
		if (name == it->first)
		{
			clog << "Texture found, using existing texture.\n";
			return it->second;
		}
	}

	sf::Texture texture;

	if(texture.loadFromFile(name))
	{
		m_textures[name] = texture;
		clog << "Loaded texture.\n";
		return m_textures[name];
	}
	else
		return errorTexture;
}

void TextureManager::remove(const std::string name)
{
	std::map<std::string, sf::Texture>::iterator itr = m_textures.find(name);
	if (itr != m_textures.end())
	{
		m_textures.erase(itr);
	} 
	else
	{
		cerr << "ERROR: Attempted to remove a texture that didn't exist!\n";
	}
}

void TextureManager::clear()
{
	if (!m_textures.empty())
		m_textures.clear();
}